import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaKE8 {
    public static void main(String[] args){
        System.out.println("===1.字符串创建和基本操作===");
        String str1="java Programming";
        String str2=new String("Hello,World");
        String str3="Java";
        String str4="JAVA";

        System.out.println("str1:"+str1);
        System.out.println("str2:"+str2);
        System.out.println("str1:"+str1.length());
        System.out.println("str4:"+str2.length());

        System.out.println("\n===2.去除空格操作===");
        String trimmedStr=str1.trim();
        System.out.println("原始字符串：["+str1+"]");
        System.out.println("去除两端空格后：["+trimmedStr+"]");

        System.out.println("\n===3.替换操作===");
        String replacedChar=str2.replace('o','0');
        String replacedStr=str2.replace("world","java");
        String replaceAll=str1.replaceAll("\\s+","-");

        System.out.println("替换字符："+replacedChar);
        System.out.println("替换字串："+replacedStr);
        System.out.println("替换所有空白："+replaceAll);

        System.out.println("\n===4.截取和分割操作===");
        String substring1=str2.substring(7);
        String substring2=str2.substring(0,5);
        System.out.println("从索引7截取："+substring1);
        System.out.println("从0截取到5："+substring2);

        String csv="apple,banana,orange,grape";
        String[] fruits=csv.split(",");
        System.out.println("分割后的水果：");
        for (String fruit:fruits){//增强型for循环，用于遍历数组fruits中的每个元素
            System.out.println(fruit);
        }

        System.out.println("\n===5.字符串判断操作===");
        System.out.println("str3是否等于str4:"+str3.equals(str4));
        System.out.println("str3是否等于str4(忽略大小写)："+str3.equalsIgnoreCase(str4));
        System.out.println("str2是否以'Hello'开头："+str2.startsWith("Hello"));
        System.out.println("str2是否以'!‘结尾："+str2.endsWith("!"));
        System.out.println("str2是否包含'World':"+str2.contains("World"));
        System.out.println("str1是否为空："+str1.isEmpty());

        System.out.println("\n====6.字符串转换操作===");
        int number=12345;
        double pi=3.14159;
        boolean flag=true;

        String numStr=String.valueOf(number);
        String piStr=String.valueOf(pi);
        String flagStr=String.valueOf(flag);

        System.out.println("int转String:"+numStr);
        System.out.println("double转String："+piStr);
        System.out.println("boolean转String："+flagStr);

        System.out.println("\n===7.字符串连接操作===");
        String concatStr=str3.concat(" ").concat("Programming");
        System.out.println("使用concat连接："+concatStr);

        String plusStr=str3+" "+"is awesome!";
        System.out.println("使用+连接："+plusStr);

        StringBuilder sb=new StringBuilder();
        sb.append(str3).append(" ").append("is").append(" ").append("powerful!");
        System.out.println("使用StringBuilder连接："+sb.toString());

        System.out.println("\n===8.字符串比较操作===");
        String a="apple";
        String b="banana";
        String c="Apple";

        System.out.println("a.compareTo(b):"+a.compareTo(b));
        System.out.println("b.compareTo(a):"+b.compareTo(a));
        System.out.println("a.compareTo(c):"+a.compareTo(c));
        System.out.println("a.compareToIgnoreCase(c):"+a.equalsIgnoreCase(c));

        System.out.println("\n===9.其他常用操作===");
        System.out.println("str2转大写："+str2.toUpperCase());
        System.out.println("str2转小写："+str2.toLowerCase());
        System.out.println("str2中‘o’第一次出现的位置："+str2.indexOf('o'));
        System.out.println("str2中'o'最后一次出现的位置："+str2.lastIndexOf('o'));
        System.out.println("str2'llo'的位置："+str2.indexOf("llo"));

        System.out.println("\n===10.字符串格式化===");
        String formattedStr=String.format("姓名：%s,年龄：%d，成绩：%.2f","张三",20,85.5);
        System.out.println(formattedStr);


        //解析字符串
        String ss="one little,two litle,three little";
        String[] str=ss.split(",");
        for (String s:str){
            System.out.println(s);
        }

        String tt="a d, m, i.n";
        String delimiters="\\s+|,\\s*|\\.\\s*";
        //分析字符串
        String[] tokensVal=tt.split(delimiters);
        //显示字符个数
        System.out.println("字符数="+tokensVal.length);
        for (String token:tokensVal){
            System.out.println(token);
        }

        //验证字符串
        //要验证的字符串
        String vv="abcdefg";
        //正则表达式规则
        String regEx="ABC*";
        //编译正则表达式
        Pattern pattern=Pattern.compile(regEx);
        //忽略大小写的写法
        //Pattern pattern=Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);;
        Matcher matcher=pattern.matcher(vv);
        //检查字符串中是否有匹配正则表达式的字符或字符串
        boolean rs=matcher.find();
        System.out.println(rs);

        //要验证的字符串
        String ww = "xyz@zbc.net";
        // 邮箱验证规则
        String regEx1 = "[a-zA-Z_]{1,}[0-9]{0,}@([a-zA-Z0-9-]+\\.){1,3}[a-zA-Z\\-]{1,}";
        // 编译正则表达式
        Pattern pattern1 = Pattern.compile(regEx1);
        // 创建 Matcher 对象
        Matcher matcher1 = pattern1.matcher(ww);
        // 检查字符串是否与正则表达式完全匹配
        boolean rs1 = matcher1.matches();
        // 输出结果
        System.out.println(rs1);


        //字符串的类型转换
        String xx="java develop,jsp develop,vb develop";
        char[] xx1=xx.toCharArray();
        System.out.println("字符数组的长度："+xx1.length);
        System.out.println("char数组中的元素是：");
        for (int i=0;i<xx.length();i++){
            System.out.println(xx1[i]+"");
        }

        boolean b1=true;
        System.out.println("布尔类型=>字符串：");
        System.out.println(String.valueOf(b1));
        int i=34;
        System.out.println("整数类型=》字符串：");
        System.out.println(String.valueOf(i));

        String yy=String.format("32的八进制：%o",32);
        System.out.println(yy);
        String yy1=String.format("字母G的小写是：%c%n",'g');
        System.out.println(yy1);
        String yy2=String.format("12>8的值：%b%n",12>8);
        System.out.println(yy2);
        String yy3=String.format("%1$d,%2$s,%3$f",125,"ddd",0.25);
        System.out.println(yy3);


        //可修改字符串
        //单线程环境
        StringBuilder stringBuilder=new StringBuilder("Hello");
        stringBuilder.append("World");
        stringBuilder.append("!");
        System.out.println("StringBuiler结果："+stringBuilder.toString());
        System.out.println("StringBuiler长度："+stringBuilder.length());
        stringBuilder.insert(5,"java");
        System.out.println("插入后："+stringBuilder);
        stringBuilder.reverse();
        System.out.println("反转后："+stringBuilder);
        System.out.println();
        //多线程环境
        StringBuffer stringBuffer=new StringBuffer("Hello");
        stringBuffer.append("World");
        stringBuffer.append("!");
        System.out.println("StringBuffer结果："+stringBuffer.toString());
        System.out.println("StringBuffer长度："+stringBuffer.length());
        stringBuffer.insert(5,"java");
        System.out.println("插入后："+stringBuffer);
        stringBuffer.reverse();
        System.out.println("反转后："+stringBuffer);

        System.out.println("\n性能比较：");
        int iterations=100000;
        long startTime,endTime;

        startTime=System.currentTimeMillis();
        StringBuilder sb1=new StringBuilder();
        for (int n=0;n<iterations;n++){
            sb1.append(n);
        }
        endTime=System.currentTimeMillis();
        System.out.println("StringBuiler耗时："+(endTime-startTime)+"ms");

        startTime=System.currentTimeMillis();
        StringBuffer sbf=new StringBuffer();
        for (int m=0;m<iterations;m++){
            sbf.append(m);
        }
        endTime=System.currentTimeMillis();
        System.out.println("StringBuffer耗时："+(endTime-startTime)+"ms");


    }
}


